package com.syqc.monitor.chart

import android.graphics.Color
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis.XAxisPosition
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.components.YAxis.AxisDependency
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.utils.ColorTemplate
import com.syqc.entity.ListItem
import com.syqc.entity.TypeInfo
import com.syqc.monitor.chart.MAxisFormat
import com.syqc.utils.Units

/**
 *@author  hfjs
 *@time   2024/11/12 10:10
 *@desc
 */
object TempDampUtil {
    val lineColors = mutableListOf<String>(
        "#00618C", "#FAC958", "#1B8DFD", "#8ECB75", "#C423D7", "#E27269", "#69D8E2", "#7569E2"
    )

    fun initTampChart(
        chart: LineChart,
        unit: String
    ) {
        //后台绘制
        chart.setDrawGridBackground(false)
        //设置描述文本
        chart.description.isEnabled = false
        //设置支持触控手势
        chart.setTouchEnabled(true)
        //设置缩放
        chart.isScaleYEnabled = false
        chart.isScaleXEnabled = true
        //设置推动
        chart.setScaleEnabled(true)
        //如果禁用,扩展可以在x轴和y轴分别完成
        chart.setPinchZoom(true)
        chart.dragDecelerationFrictionCoef = 0.9f
        chart.isHighlightPerDragEnabled = true
        chart.animateX(1500)
        //是否显示图例说明
        chart.legend.isEnabled = false
        initYAxis(chart.axisLeft, "#333333", unit, false)
    }


    /**
     * 初始化油量
     */
    private fun initYAxis(yAxis: YAxis, color: String, unit: String, isZero: Boolean = true) {
        yAxis.axisMinimum = 0f
        // vertical grid lines
        yAxis.enableGridDashedLine(10f, 10f, 0f)
        yAxis.isGranularityEnabled = true
        yAxis.axisLineColor = ColorTemplate.rgb(color)
        yAxis.textColor = ColorTemplate.rgb(color)
        yAxis.valueFormatter = MAxisFormat(unit)
        if (isZero) {
            // 设置y轴的最小值
            yAxis.axisMinimum = 0f
        } else {
            yAxis.setStartAtZero(false)
        }
    }

    /**
     * 初始化时间
     */
    fun initXAxis2(chart: LineChart, values: Collection<String>) {
        val xAxis = chart.xAxis
        xAxis.position = XAxisPosition.BOTTOM
        // vertical grid lines
        xAxis.enableGridDashedLine(10f, 10f, 0f)
        xAxis.granularity = 1f // only intervals of 1 day
        xAxis.labelCount = 5
        xAxis.textSize = 10f
        xAxis.valueFormatter = IndexAxisValueFormatter(values)
    }

    /**
     * 设置温度数据
     */
    fun setTampData(
        chart: LineChart,
        list: MutableList<ListItem>,
        types: MutableList<TypeInfo>,
        data: HashMap<String, MutableList<Entry>>,
        unit: String
    ): ArrayList<LineDataSet> {
        val arrayListOf = arrayListOf<LineDataSet>()
        data.keys.reversed().sorted().forEachIndexed { index, item ->
            arrayListOf.add(
                getLineDataSet(
                    data[item]!!,
                    if (index > 1) AxisDependency.LEFT else AxisDependency.RIGHT,
                    lineColors[index]
                )
            )
        }
        val mv = MChartMarker4(chart.context, list, types, unit)
        mv.chartView = chart // For bounds control
        chart.marker = mv
        return arrayListOf
    }

    /**
     * 获取设置
     */
    private fun getLineDataSet(
        data: MutableList<Entry>,
        dependency: AxisDependency,
        color: String
    ): LineDataSet {
        val lineDataSet = LineDataSet(data, "")
        lineDataSet.axisDependency = dependency
        lineDataSet.lineWidth = 2f
        lineDataSet.setDrawCircles(false)
        lineDataSet.color = ColorTemplate.rgb(color)
        return lineDataSet
    }

    /**
     * 改变数据
     */
    fun change2LineData(chart: LineChart, list: MutableList<LineDataSet>, isMile: Boolean = false) {
        val rightDataSet = if (isMile) {
            initYAxis(chart.axisRight, lineColors[0], Units.km)
            list[0]
        } else {
            initYAxis(chart.axisRight, lineColors[1], Units.speed)
            list[1]
        }
        val newList = mutableListOf<LineDataSet>()
        list.forEachIndexed { index, item -> if (index > 1) newList.add(item) }
        newList.add(0, rightDataSet)
        val data = LineData(newList.toList())
        data.setValueTextColor(Color.BLACK)
        data.setValueTextSize(9f)
        chart.data = data
        chart.invalidate()
    }

}